public class MinWindowToCoverTargetCharacters {
    public String minWindow(String s, String t) {
        int lenS = s.length();
        int lenT = t.length();
        int sTimes[] = new int[128];
        int tTimes[] = new int[128];

        for (char c : t.toCharArray()) {
            tTimes[c]++;
        }

        int left = 0, right = 0, resLen = Integer.MAX_VALUE, resLeft = 0;
        int count = 0;
        while (right < lenS) {
            char c = s.charAt(right);
            if (tTimes[c] == 0) {
                right++;
                continue;
            }
            right++;
            if (sTimes[c] < tTimes[c]) {
                count++;
            }

            sTimes[c]++;
            while (count == lenT) {
                if (right - left < resLen) {
                    resLeft = left;
                    resLen = right - left;
                }
                char delete = s.charAt(left);
                left++;
                if (tTimes[delete] != 0) {
                    if (sTimes[delete] == tTimes[delete]) {
                        count--;
                    }
                    sTimes[delete]--;
                }

            }
        }
        return resLen == Integer.MAX_VALUE ? "" : s.substring(resLeft, resLeft + resLen);
    }
}
